/*
 * @Author: shanghongyun 2879455630@qq.com
 * @Date: 2025-05-29 16:50:34
 * @LastEditors: shanghongyun 2879455630@qq.com
 * @LastEditTime: 2025-06-18 10:11:56
 * @FilePath: \A05\Dev\dev.h
 * @Description: 外设管理
 */
#ifndef __DEV_H_
#define __DEV_H_

#include "NM1120.h"
#include "log.h"

/* **************************GPIO************************** */
/*KEY*/
#define KEYR_PORT                       PC
#define KEYR_PIN                        BIT4
#define Get_KeyR()                      PC4

#define KEYL_PORT                       PA
#define KEYL_PIN                        BIT2
#define Get_KeyL()                      PA2

/*CHARGE EN OR DIS*/
#define CHARGE_L_PORT                   PA 
#define CHARGE_L_PIN                    BIT5
#define EN_CHARGE_L()                   (PA5 = 1)
#define DIS_CHARGE_L()                  (PA5 = 0)

#define CHARGE_R_PORT                   PD
#define CHARGE_R_PIN                    BIT5
#define EN_CHARGE_R()                   (PD5 = 1)
#define DIS_CHARGE_R()                  (PD5 = 0)

/*display:software i2c communication*/
#define DISPLAY_SCL_PORT                PA
#define DISPLAY_SCL_PIN                 BIT0
#define DISPLAY_SCL_HIGH                (PA0 = 1)
#define DISPLAY_SCL_LOW                 (PA0 = 0)


#define DISPLAY_SDA_PORT                PA
#define DISPLAY_SDA_PIN                 BIT1
#define DISPLAY_SDA_HIGH                (PA1 = 1)
#define DISPLAY_SDA_LOW                 (PA1 = 0)

/*QC Protocol*/
#define QC_PORT                         PC           
#define QC_PIN                          BIT3

#define QC_HIGH                         (PC3 = 1)
#define QC_LOW                          (PC3 = 0)


/*电池长度检测*/
#define L_BAT_LENTH_DET_PORT            PD
#define L_BAT_LENTH_DET_PIN             BIT3
#define L_GET_BAT_LENTH()               PD3

#define R_BAT_LENTH_DET_PORT            PD
#define R_BAT_LENTH_DET_PIN             BIT4
#define R_GET_BAT_LENTH()               PD4

/* **************************EPWM************************** */
#include "systick.h"
#define EPWM_Fre                        (90000UL)       /*EPWM频率 单位:Hz*/
#define EPWM_PERIOD                     ((SYS_Frequency / EPWM_Fre) - 1)        /*epwm重装载值*/
#define EPWM_DUTY(x)                    (((x) * EPWM_PERIOD) / 100)
#define EPWM_DUTY_1000(x)               (((x) * EPWM_PERIOD) / 1000)

#define EPWM_MAX_DUTY                    EPWM_DUTY(47)
#define EPWM_MIN_DUTY                    EPWM_DUTY(1)


/* **************************ADC************************** */
/*
*   VOUT_L      ADC0_0  
*   VBAT_L      ADC0_1
*   CUR_R       ADC0_2
*   VBAT_R      ADC0_4
*   CIN         ADC0_3
*
*   CUR_L       ADC1_5
*   PCB_TEMP    ADC1_2
*   VOUT_R      ADC1_0
*   
*/
#define ADC0_VOUT_L_Channel             (0 << EADC_CTL_ADC0CHSEL_Pos)
#define ADC0_VBAT_L_Channel             (1 << EADC_CTL_ADC0CHSEL_Pos)
#define ADC0_CUR_R_Channel              (2 << EADC_CTL_ADC0CHSEL_Pos)
#define ADC0_CIN__Channel               (3 << EADC_CTL_ADC0CHSEL_Pos)
#define ADC0_VBAT_R_Channel             (4 << EADC_CTL_ADC0CHSEL_Pos)


#define ADC1_VOUT_R_Channel             (0 << EADC_CTL_ADC1CHSEL_Pos)
#define ADC1_PCB_TEMP_Channel           (2 << EADC_CTL_ADC1CHSEL_Pos)
#define ADC1_CUR_L_Channel              (5 << EADC_CTL_ADC1CHSEL_Pos)


#define	EADC_CTL_ADC01CHSEL_MSK	        (~(EADC_CTL_ADC0CHSEL_Msk | EADC_CTL_ADC1CHSEL_Msk ))//ADC0 1输入通道清除


void dev_init(void);

void gpio_config(void);
void epwm_config(void);
void eadc_config(void);
void PGA_config(void);


extern uint8_t gEADC_Convert_Pos;
extern uint32_t gEADC_convert_Seq[];
#define EADC_CONVERT_POS_MAX            (4)

extern uint8_t gDisplay_refresh;

#endif

